<?php
namespace app\services;

class MaterialService extends BaseService {
    public $matrix = [
        'id'                => 'id',
        'name'              => 'name',
        'type'              => 'ext',
        'width'             => 'width',
        'height'            => 'height',
        'size'              => 'size',
        'belongUser'        => 'advertiser',
        'status'            => ['status', '$v==0 && $v=\Yii::t("material", "pending");
                                           $v==1 && $v=\Yii::t("material", "pass");
                                           $v==2 && $v=\Yii::t("material", "reject");
                                           return $v;'],
        'lastModifiedDate' => 'last_update',
        'operate' => ['id', 'return "edit,view";'],
    ];

    public function getAllMaterials() {
        $materials = $this->getAll('Material', []);
        $advertisers = $this->getAll('Advertiser', []);
        foreach ($materials as &$material) {
            $material['size'] = $material['width'].'x'.$material['height'];
            $material['advertiser'] = $this->t('common', 'unknown');
            $adid = $material['advertiser_id'];
            if (is_array($advertisers[$adid])) {
                $material['advertiser'] = $advertisers[$adid]['name'];
            }
        }
        return $this->fieldsMap($this->matrix, $materials);
    }    
}//end
